﻿<%@ Page Language="C#" AutoEventWireup="false" CodeFile="recImpositionform.aspx.cs"
    Inherits="SIPO.Web.Forms.Transaction.recImpositionform" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Imposition Form</title>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-id.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>

    <script src="<%= ResolveUrl("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.wrapper.js") %>" type="text/javascript"></script>

    <script src="<%= ResolveUrl("~/scripts/jquery.fileupload.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.iframe-transport.js") %>" type="text/javascript"></script>    

    <script src="<%= ResolveUrl("~/scripts/accounting.min.js") %>" type="text/javascript"></script>

    <style type="text/css">
        .ui-autocomplete
        {
            max-height: 100px;
            overflow-y: auto; /* prevent horizontal scrollbar */
            overflow-x: hidden; /* add padding to account for vertical scrollbar */
            padding-right: 20px;
        }
        .custom-combobox
        {
            position: relative;
            display: inline-block;
        }
        .custom-combobox-toggle
        {
            position: absolute;
            top: 0;
            bottom: 0;
            margin-left: -1px;
            padding: 0; /* support: IE7 */
            height: 1.7em;
            top: 0.1em;
        }
        .custom-combobox-input
        {
            margin: 0;
            padding: 0.3em;
        }
        .style1
        {
            width: 225px;
        }
        .style2
        {
            width: 111px;
        }
    </style>
    <script type="text/javascript" language="javascript">

        (function ($) {

            $.widget("custom.combobox", {
                _create: function () {
                    this.wrapper = $("<span>")
          .addClass("custom-combobox")
          .insertAfter(this.element);

                    this.element.hide();
                    this._createAutocomplete();
                    this._createShowAllButton();
                },

                _createAutocomplete: function () {
                    var selected = this.element.children(":selected"),
          value = selected.val() ? selected.text() : "";

                    var width = this.element.width() > 100 ? "style='width:" + this.element.width() + "px';" : "";

                    this.input = $("<input " + width + ">")
          .appendTo(this.wrapper)
          .val(value)
          .attr("title", "")
          .addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left")
          .autocomplete({
              autoFocus: true,
              selectFirst: true,
              delay: 0,
              minLength: 0,
              source: $.proxy(this, "_source")
          })
          .tooltip({
              tooltipClass: "ui-state-highlight"
          });

                    this._on(this.input, {
                        autocompleteselect: function (event, ui) {
                            ui.item.option.selected = true;
                            this._trigger("select", event, {
                                item: ui.item.option
                            });

                            var idOfCaller = this.element.attr('id');
                            switch (idOfCaller.toLowerCase()) {
                                case ("ddldebtor"):
                                    $('#ddlDebtor').change();
                                    break;
                                case ("ddlsubsubsector"):
                                    $('#ddlSubSubSector').change();
                                    break;
                                default:
                                    break;
                            }

                        },

                        autocompletechange: "_removeIfInvalid"
                    });
                },

                _createShowAllButton: function () {
                    var input = this.input,
          wasOpen = false;

                    $("<a>")
          .attr("tabIndex", -1)
          .attr("title", "Tampilkan Semua Item")
          .tooltip()
          .appendTo(this.wrapper)
          .button({
              icons: {
                  primary: "ui-icon-triangle-1-s"
              },
              text: false
          })
          .removeClass("ui-corner-all")
          .addClass("custom-combobox-toggle ui-corner-right")
          .mousedown(function () {
              wasOpen = input.autocomplete("widget").is(":visible");
          })
          .click(function () {
              input.focus();

              // Close if already visible
              if (wasOpen) {
                  return;
              }

              // Pass empty string as value to search for, displaying all results
              input.autocomplete("search", "");
          });
                },

                _source: function (request, response) {
                    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                    response(this.element.children("option").map(function () {
                        var text = $(this).text();
                        if (this.value && (!request.term || matcher.test(text)))
                            return {
                                label: text,
                                value: text,
                                option: this
                            };
                    }));
                },

                _removeIfInvalid: function (event, ui) {

                    // Selected an item, nothing to do
                    if (ui.item) {
                        return;
                    }

                    // Search for a match (case-insensitive)
                    var value = this.input.val(),
          valueLowerCase = value.toLowerCase(),
          valid = false;
                    this.element.children("option").each(function () {
                        if ($(this).text().toLowerCase() === valueLowerCase) {
                            this.selected = valid = true;
                            return false;
                        }
                    });

                    // Found a match, nothing to do
                    if (valid) {
                        return;
                    }

                    // Remove invalid value
                    this.input
          .val("")
          .attr("title", value + " tidak ditemukan")
          .tooltip("open");
                    this.element.val("");
                    this._delay(function () {
                        this.input.tooltip("close").attr("title", "");
                    }, 2500);
                    this.input.data("ui-autocomplete").term = "";
                },

                _destroy: function () {
                    this.wrapper.remove();
                    this.element.show();
                },
                autocomplete: function (value, text) {
                    this.element.val(value);
                    this.input.val(text);
                }
            });
        })(jQuery);

        $(function () {
            $("#ddlDebtor").combobox();
            $("#ddlSubSubSector").combobox();
        });

    </script>
    <script type="text/javascript">
        $(function () {

            $('#form1').fileupload({ autoUpload: false, replaceFileInput: false, singleFileUploads: true });

        });
    </script>
    <script type="text/javascript">

        $(document).ready(function () {

            $("#btnSave").button({ icons: { primary: "ui-icon-disk"} });
            $("#btnClose").button({ icons: { primary: "ui-icon-close"} });

            $('#btnClose').click(function () {
                parent.$('#dialog').dialog("close");

                return false;
            });

        });
 
    </script>
    <script type="text/javascript">
        $(document).ready(function () {

            $('#ddlDebtor').combobox('autocomplete', "", "-- pilih debitur --");
            $('#ddlSubSubSector').combobox('autocomplete', "", "-- pilih sub sub sektor --");
      
            $.validator.addMethod("dropDownValidator", function (value, element, parameters) {
                return (value != '');
            });

           
            $('#ddlSubSubSector').change(function () {
                var debtorId = $('#ddlDebtor option:selected').val();
                var subsubsectorId = $('#<%= ddlSubSubSector.ClientID%> option:selected').val();
                $subSubSectors = $('#ddlSubSubSector');
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/gettarifbysubsubsectorid.ashx?subSubSectorId=' + subsubsectorId + '&transtype=iuranberkala&debtorId=' + debtorId + ' ") %>';
                if (subsubsectorId > 0) {
                    jQuery.ajax({
                        type: 'POST',
                        url: handlerUrl,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            if (data.TariffID != 0) {
                                $('#<%= hfTarifId.ClientID %>').val(data.TariffID);
                                $('#txtPercentage').val(data.Percentage);
                                if (data.MaxValue.toString().indexOf(',') > 0)
                                    $('#txtTarifMax').val(data.MaxValue.substr(0, data.MaxValue.toString().indexOf(',')));
                                else if (data.MaxValue.toString().indexOf('.') > 0)
                                    $('#txtTarifMax').val(data.MaxValue.substr(0, data.MaxValue.toString().indexOf('.')));
                                if (data.MinValue.toString().indexOf(',') > 0)
                                    $('#txtTarifMin').val(data.MinValue.substr(0, data.MinValue.toString().indexOf(',')));
                                else if (data.MinValue.toString().indexOf('.') > 0)
                                    $('#txtTarifMin').val(data.MinValue.substr(0, data.MinValue.toString().indexOf('.')));
                                $('#<%= hfDebtorSectorId.ClientID %>').val(data.DebtorSectorId);
                                if (data.Value.toString().indexOf(',') > 0)
                                    $('#<%= hfTarifValue.ClientID %>').val(data.Value.substr(0, data.Value.toString().indexOf(',')));
                                else if (data.Value.toString().indexOf('.') > 0)
                                    $('#<%= hfTarifValue.ClientID %>').val(data.Value.substr(0, data.Value.toString().indexOf('.')));
                                else
                                    $('#<%= hfTarifValue.ClientID %>').val(data.Value);

                                var unformattarifMax = accounting.unformat($('#txtTarifMax').val(), ",", "."); // Revert to en-US format
                                $('#txtTarifMax').val(accounting.formatNumber(unformattarifMax, 2, ".", ",")); // Set to id-ID format

                                var unformattarifMin = accounting.unformat($('#txtTarifMin').val(), ",", "."); // Revert to en-US format
                                $('#txtTarifMin').val(accounting.formatNumber(unformattarifMin, 2, ".", ",")); // Set to id-ID format
                            }
                            else {
                                alert('Jenis Usaha ini belum ada Tarif nya!');
                                //$subSubSectors.get(0).options.length = 0;
                                //$subSubSectors.get(0).options[0] = new Option("-- pilih sub sub sector --", "");
                                $('#ddlSubSubSector').combobox('autocomplete', "", "-- pilih sub sub sektor --");
                                $('#<%= hfTarifId.ClientID %>').val('');
                                $('#txtPercentage').val('');
                                $('#txtTarifMax').val('');
                                $('#txtTarifMin').val('');
                                $('#<%= hfDebtorSectorId.ClientID %>').val('');
                                $('#<%= hfTarifValue.ClientID %>').val('');
                            }
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }
                else {
                    $('#<%= hfTarifId.ClientID %>').val('');
                    $('#txtPercentage').val('');
                    $('#txtTarifMax').val('');
                    $('#txtTarifMin').val('');
                    $('#<%= hfDebtorSectorId.ClientID %>').val('');
                    $('#<%= hfTarifValue.ClientID %>').val('');
                }
            });

            $('#ddlDebtor').change(function () {
                var debtorId = $('#ddlDebtor option:selected').val();
                $subSubSectors = $('#ddlSubSubSector');
                if (debtorId > 0) {
                    var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getsubsubsector.ashx?method=getbyDebtorId&debtorid=") %>';
                    $.ajax({
                        type: 'POST',
                        url: handlerUrl + debtorId,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (datasubsubs) {
                            $subSubSectors.get(0).options.length = 0;
                            $subSubSectors.get(0).options[0] = new Option("-- pilih sub sub sektor --", "");
                            $.each(datasubsubs, function (i, datasubsub) {
                                $subSubSectors.append('<option value="' + datasubsub.SubSubSectorID + '">' + datasubsub.SubSubSectorCode + ' - ' + datasubsub.SubSubSectorName + '</option>');
                            });
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }
                else {
                    $subSubSectors.get(0).options.length = 0;
                    $subSubSectors.get(0).options[0] = new Option("-- pilih sub sub sektor --", "");
                }
            });

        });

        
        
    
    </script>
    <script type="text/javascript">

        $(document).ready(function () {

            $.validator.addMethod("dropDownValidator", function (value, element, parameters) {
                return (value != '');
            });
            // 1. prepare the validation rules and messages.
            var rules = {
                ddlDebtor: { dropDownValidator: true },
                ddlSubSubSector: { dropDownValidator: true }
                //txtDebtorValue: "number",
                //txtOJKValue: "number"
            };

            var messages = {
                ddlDebtor: "Debitur harus dipilih",
                ddlSubSubSector: "Sub sub sektor harus dipilih"
                //txtDebtorValue: "Nilai pengenaan Debitur harus di isi dengan angka",
                //txtOJKValue: "Nilai pengenaan OJK harus di isi dengan angka"
            };
            // 2. Initiate the validator
            var validator = new jQueryValidatorWrapper("form1", rules, messages);
            var numeric = /[0-9,.]+$/;
            $('#txtDebtorValue').bind('keyup blur', function () {
                var match = String($(this).val()).match(numeric);
                if (!match)
                    $(this).val($(this).val().replace(/[^0-9.,]/g, ''))

            });

            $('#txtDebtorValue').blur(function () {
                var unformat = accounting.unformat($(this).val(), ",", "."); // Revert to en-US format
                $(this).val(accounting.formatNumber(unformat, 2, ".", ",")); // Set to id-ID format
            });

            $('#txtOJKValue').bind('keyup blur', function () {
                var match = String($(this).val()).match(numeric);
                if (!match)
                    $(this).val($(this).val().replace(/[^0-9.,]/g, ''))

            });

            $('#txtOJKValue').blur(function () {
                var unformat = accounting.unformat($(this).val(), ",", "."); // Revert to en-US format
                $(this).val(accounting.formatNumber(unformat, 2, ".", ",")); // Set to id-ID format
            });

            // 3. Set the click event to do the validation
            $("#btnSave").click(function (e) {
                e.preventDefault();

                if (!validator.validate())
                    return;
                var debtorid = $('#ddlDebtor option:selected').val();
                var subsubsectorid = $('#ddlSubSubSector option:selected').val();
                var tahunlapkeu = $('#ddlTahunLapKeu option:selected').val();
                var impositionbaseid = $('#ddlImpositionBase option:selected').val();
                var percentage = $('#txtPercentage').val();
                var tarifmin = $('#txtTarifMin').val();
                var tarifmax = $('#txtTarifMax').val();
                var debtorvalue = $('#txtDebtorValue').val();
                var ojkvalue = $('#txtOJKValue').val();
                var id = $('#<%= hfID.ClientID %>').val();
                var tarifid = $('#<%= hfTarifId.ClientID %>').val();
                var debtorsectorid = $('#<%= hfDebtorSectorId.ClientID %>').val();
                var tarifvalue = $('#<%= hfTarifValue.ClientID %>').val();

                //                var objrecImposition = new Object();
                //                objrecImposition.ImpositionID = id;
                //                objrecImposition.DebtorID = encodeURIComponent(debtorid);
                //                objrecImposition.SubSubSectorID = encodeURIComponent(subsubsectorid);
                //                objrecImposition.TahunLapKeu = encodeURIComponent(tahunlapkeu);
                //                objrecImposition.ImpositionBaseID = encodeURIComponent(impositionbaseid);
                //                objrecImposition.Percentage = encodeURIComponent(percentage);
                //                objrecImposition.Minimal = encodeURIComponent(tarifmin);
                //                objrecImposition.Maximal = encodeURIComponent(tarifmax);
                //                objrecImposition.DebtorValue = encodeURIComponent(debtorvalue);
                //                objrecImposition.OJKValue = encodeURIComponent(ojkvalue);
                //                objrecImposition.TarifID = encodeURIComponent(tarifid);
                //                objrecImposition.DebtorSectorID = encodeURIComponent(debtorsectorid);
                //                objrecImposition.TarifValue = encodeURIComponent(tarifvalue);

                //                jsonData = JSON.stringify(objrecImposition);

                //                var handlerUrl = '<%= ResolveClientUrl("~/ajaxhandler/transaction/recImpositionhandler.ashx") %>';

                //                if (id == 0)
                //                    CallAjax(handlerUrl, "?method=insert", jsonData);
                //                else {
                //                    CallAjax(handlerUrl, "?method=update", jsonData);
                //                }

                //                parent.$('#dialog').dialog('close');

                //                setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);

                //                return false;
                if (id == 0) {

                    if ($('#fileUpload').val() == '') { // Insert without uploadedfile

                        $.post('<%= ResolveUrl("~/AjaxHandler/Transaction/recImpositionhandler.ashx") %>' + '?method=insert',
                                $("#form1")
                                .find("input,textarea,select,hidden")
                                .not("#__VIEWSTATE,#__EVENTVALIDATION")
                                .serialize())
                            .done(function (data) {
                                var r = JSON.parse(data);
                                alert(r.Message);
                                //location.href = '<%= ResolveUrl("~/lists/Transaction/recImpositionAlllist.aspx") %>';
                            })
                            .fail(function () {
                                alert('Data gagal di simpan!');
                            });

                    }
                    else {

                        $('#form1').fileupload('send', {
                            fileInput: $('#fileUpload'),
                            dataType: 'json',
                            url: '<%= ResolveUrl("~/AjaxHandler/Transaction/recImpositionhandler.ashx") %>' + '?method=insert'
                        })
                        .done(function (e, data) {
                            var x = JSON.stringify(e); // Change from Javascript Object into JSON
                            var y = JSON.parse(x); // Change from JSON into Javascript String
                            alert(y.Message);
                            //location.href = '<%= ResolveUrl("~/lists/Transaction/recImpositionAlllist.aspx") %>';
                        })
                        .fail(function (e, data) {
                            alert("Data gagal di simpan !");
                        });
                    }

                }
                else {

                    if ($('#fileUpload').val() == '') { // Update without uploadedfile

                        $.post('<%= ResolveUrl("~/AjaxHandler/Transaction/recImpositionhandler.ashx") %>' + '?method=update',
                                $("#form1")
                                .find("input,textarea,select,hidden")
                                .not("#__VIEWSTATE,#__EVENTVALIDATION")
                                .serialize())
                            .done(function (data) {
                                var r = JSON.parse(data);
                                alert(r.Message);
                            })
                            .fail(function () {
                                alert('Data gagal di ubah!');
                            });

                    }
                    else {

                        $('#form1').fileupload('send', {
                            fileInput: $('#fileUpload'),
                            dataType: 'json',
                            url: '<%= ResolveUrl("~/AjaxHandler/Transaction/recImpositionhandler.ashx") %>' + '?method=update'
                        })
                        .done(function (e, data) {
                            var x = JSON.stringify(e); // Change from Javascript Object into JSON
                            var y = JSON.parse(x); // Change from JSON into Javascript String
                            alert(y.Message);
                        })
                        .fail(function (e, data) {
                            alert("Data gagal di ubah !");
                        });

                    }
                }

                parent.$('#dialog').dialog('close');

                setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);

                return false;
            });
        });
    
    </script>
    <script type="text/javascript">

        function ClearText() {
            $('#txtPercentage').val('');
        }
        
    </script>
    <style type="text/css">
        .style1
        {
            width: 8px;
        }
        .style2
        {
            width: 165px;
        }
    </style>
</head>
<body onload="ClearText();">
    <form id="form1" runat="server">
    <table border="0">
        <tr>
            <td class="style1">
                &nbsp;
            </td>
            <td style="width: 50">
                Debitur
            </td>
            <td>
                :
            </td>
            <td colspan="2">
                <asp:DropDownList ID="ddlDebtor" runat="server" Width="172">
                </asp:DropDownList>
                &nbsp;<span style="color: Red;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*</span>
            </td>
        </tr>
        <tr>
            <td class="style1">
                &nbsp;
            </td>
            <td style="width: 50">
                Sub Sub Sektor
            </td>
            <td>
                :
            </td>
            <td colspan="2">
                <asp:DropDownList ID="ddlSubSubSector" runat="server" Width="172">
                </asp:DropDownList>
                &nbsp;<span style="color: Red;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*</span>
            </td>
        </tr>
        <tr>
            <td class="style1">
                &nbsp;
            </td>
            <td style="width: 50">
                Tahun Laporan Keuangan
            </td>
            <td>
                :
            </td>
            <td class="style2">
                <asp:DropDownList ID="ddlTahunLapKeu" runat="server" Width="100">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="style1">
                &nbsp;
            </td>
            <td style="width: 50">
                Dasar Pengenaan
            </td>
            <td>
                :
            </td>
            <td class="style2">
                <asp:DropDownList ID="ddlImpositionBase" runat="server" Width="200">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="style1">
                &nbsp;
            </td>
            <td style="width: 50">
                Tarif
            </td>
            <td>
                :
            </td>
            <td class="style2">
                <asp:TextBox ID="txtPercentage" runat="server" Width="40" ReadOnly="true"></asp:TextBox>
            </td>
            &nbsp;&nbsp;
            <td>
                &nbsp;&nbsp; Min
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtTarifMin" runat="server" Width="100" ReadOnly="true"></asp:TextBox>
            </td>
            <td>
                Max
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtTarifMax" runat="server" Width="100" ReadOnly="true"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                &nbsp;
            </td>
            <td style="width: 50">
                Nilai Pengenaan Debitur
            </td>
            <td>
                :
            </td>
            <td class="style2">
                <asp:TextBox ID="txtDebtorValue" runat="server" Width="200" Style="text-align: right"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                &nbsp;
            </td>
            <td style="width: 50">
                Nilai Pengenaan OJK
            </td>
            <td>
                :
            </td>
            <td class="style2">
                <asp:TextBox ID="txtOJKValue" runat="server" Width="200" Style="text-align: right"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                &nbsp;
            </td>
            <td style="width: 50">
                Lampiran
            </td>
            <td>
                :
            </td>
            <td class="style2">
                <input type="file" id="fileUpload" />
            </td>
        </tr>
    </table>
    <table border="0">
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td>
                <div style="text-align: right; padding-top: 10px;">
                    <button id="btnSave" type="button">
                        Simpan</button>
                    <button id="btnClose" type="button">
                        Batal</button>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td>
                <asp:HiddenField ID="hfID" runat="server" />
                <asp:HiddenField ID="hfDebtorSectorId" runat="server" />
                <asp:HiddenField ID="hfTarifId" runat="server" />
                <asp:HiddenField ID="hfTarifValue" runat="server" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
